home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / mms / mmsauth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-03  |  3.5 KB  |  128 lines

  1. /*////////////////////////////////////////////////////////////////////////
  2. Copyright (c) 1992 Electrotechnical Laboratry (ETL)
  3.  
  4. Permission to use, copy, modify, and distribute this material 
  5. for any purpose and without fee is hereby granted, provided 
  6. that the above copyright notice and this permission notice 
  7. appear in all copies, and that the name of ETL not be 
  8. used in advertising or publicity pertaining to this 
  9. material without the specific, prior written permission 
  10. of an authorized representative of ETL.
  11. ETL MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
  12. OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
  13. WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
  14. /////////////////////////////////////////////////////////////////////////
  15. Content-Type: program/C; charset=US-ASCII
  16. Program:      mmsauth.c (metamail client authentication)
  17. Author:       Yutaka Sato <ysato@etl.go.jp>
  18.  
  19. Description:
  20.    When mms invoked as remote server, it must run under someone's access
  21. permission. This module authenticate the remote client based on the set
  22. of {user, password file name, password}.
  23.  
  24. History:
  25.   v0.1    92.04.24     created a small prototype
  26. ///////////////////////////////////////////////////////////////////////*/
  27.  
  28.  
  29. /*////////////////////////////////////////////////////////////////////////
  30.  *    Server side authentication
  31.  */
  32. #include <stdio.h>
  33. #include <sys/param.h>
  34. #include <pwd.h>
  35. #include <sys/stat.h>
  36. #define MMS_SALT "MM"
  37.  
  38. struct passwd *
  39. MMS_user(user,file,passwd,errmsg)
  40.     char *user,*file,*passwd,*errmsg;
  41. {    int euid;
  42.     char hostname[MAXPATHLEN];
  43.     char tmp[MAXPATHLEN],xpasswd[MAXPATHLEN],*upasswd;
  44.     struct passwd *pw;
  45.     FILE *fp;
  46.     struct stat Stat;
  47.     int rcc;
  48.  
  49.     pw = getpwnam(user);
  50.     if( pw == NULL ){
  51.         sprintf(errmsg,"unknown user(%s)",file);
  52.         return 0;
  53.     }
  54.     euid = pw->pw_uid;
  55.  
  56.     if( MMS_privileged_user(euid,pw->pw_gid) ){
  57.         sprintf(errmsg,"don't use in privileged user(uid=%d,gid=%d)",
  58.             pw->pw_uid,pw->pw_gid);
  59.         return 0;
  60.     }
  61.  
  62.     if( *file != '/' ){
  63.         sprintf(tmp,"%s/%s",pw->pw_dir,file);
  64.         strcpy(file,tmp);
  65.     }
  66.     fp = fopen(file,"r");
  67.     if( fp == NULL ){
  68.         sprintf(errmsg,"unknown file(%s)",file);
  69.         return 0;
  70.     }
  71.     fstat(fileno(fp),&Stat);
  72.     if( Stat.st_uid != euid ||(Stat.st_mode & 0777) & ~0700 ){
  73.         sprintf(errmsg,
  74.             "file must be readable only for %s (%s) %d/%d",
  75.             user,file,Stat.st_uid,euid);
  76.         return 0;
  77.     }
  78.     rcc = read(fileno(fp),xpasswd,11);
  79.     fclose(fp);
  80.     if( rcc < 11 ){
  81.         sprintf(errmsg,"encoded passwd too short ? (rcc)",rcc);
  82.         return 0;
  83.     }
  84.  
  85.     upasswd = (char*)crypt(passwd,MMS_SALT) + 2;
  86.     if( strncmp(upasswd,xpasswd,11) != 0 ){
  87.         sprintf(errmsg,"incorrect passwd %s (%s)",passwd,upasswd);
  88.         return 0;
  89.     }
  90.     if( setreuid(euid,euid) != 0 ){
  91.         sprintf(errmsg,"cannot set uid (%s:%d).",user,euid);
  92.         return 0;
  93.     }
  94.     return pw;
  95. }
  96.  
  97. /*////////////////////////////////////////////////////////////////////////
  98.  *    Client side default authentication
  99.  */
  100. #define MMS_CLNT_AUTH    ".mmsprofile"
  101. #define MMS_SERV_AUTH    ".mmspasswd"
  102. MMS_authenticate(serv)
  103.     FILE *serv[];
  104. {    FILE *afp;
  105.     char *user,authfile[MAXPATHLEN],password[MAXPATHLEN];
  106.     char *getenv();
  107.  
  108.     sprintf(authfile,"%s/%s",getenv("HOME"),MMS_CLNT_AUTH);
  109.     afp = fopen(authfile,"r");
  110.     if( afp == NULL )
  111.         return;
  112.     password[0] = 0;
  113.     fscanf(afp,"%[^\n]",password);
  114.     fclose(afp);
  115.  
  116.     user = getenv("USER");
  117.     MMS_putserver(serv,"USER %s %s %s\n",user,MMS_SERV_AUTH,password);
  118.     MMS_getserver(serv);
  119. }
  120.  
  121. /*////////////////////////////////////////////////////////////////////////
  122.  */
  123. MMS_privileged_user(uid,gid){
  124.     if( uid == 0 || gid == 0 )
  125.         return 1;
  126.     return 0;
  127. }
  128.